package com.example.musicplayer.square;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.musicplayer.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Square extends Activity implements
        OnItemClickListener, OnItemLongClickListener {

    private Context mContext;
    private ListView listview;
    private SQLiteDatabase db;
    private SimpleAdapter sdata;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.square);

        View sq_a, sq_b;
        sq_a = findViewById(R.id.sq_a);
        sq_b = findViewById(R.id.sq_b);
        sq_a.setVisibility(View.VISIBLE);
        sq_b.setVisibility(View.GONE);


        Refresha();
        listview.setOnItemClickListener(this);
        listview.setOnItemLongClickListener(this);

    }


    public void Refresha() {
        DBSquare Square = new DBSquare(this);
        db = Square.getWritableDatabase();
        //使用游标进行挨个查找  rawQuery方法就是一种逐行查找的方法
        Cursor cursor = db.rawQuery("select * from Square", new String[]{});
        listview = findViewById(R.id.listview);
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        while (cursor.moveToNext()) {
            Map<String, Object> showitem = new HashMap<String, Object>();
            showitem.put("id", cursor.getString(0));
            showitem.put("sshuo", cursor.getString(2));
            showitem.put("mname", cursor.getString(3));
            showitem.put("comment1", cursor.getString(4));
            list.add(showitem);
        }
        sdata = new SimpleAdapter(this, list, R.layout.square_item,
                new String[]{"sshuo", "mname", "comment1"}, new int[]{
                R.id.tv_date, R.id.tv_name, R.id.tv_com1});
        listview.setAdapter(sdata);
    }

    public void Refreshb() {
        DBSquare Square = new DBSquare(this);
        db = Square.getWritableDatabase();


        Bundle bundle = this.getIntent().getExtras();
        String manid = bundle.getString("manid");

        //使用游标进行挨个查找  rawQuery方法就是一种逐行查找的方法
        Cursor cursor = db.rawQuery("select * from Square where mid=?", new String[]{manid});
        listview = findViewById(R.id.listview2);
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        while (cursor.moveToNext()) {
            Map<String, Object> showitem = new HashMap<String, Object>();
            showitem.put("id", cursor.getString(0));
            showitem.put("sshuo", cursor.getString(2));
            showitem.put("mname", cursor.getString(3));
            showitem.put("comment1", cursor.getString(4));
            list.add(showitem);
        }
        SimpleAdapter sdata = new SimpleAdapter(this, list, R.layout.square_item,
                new String[]{"sshuo", "mname", "comment1"}, new int[]{
                R.id.tv_date, R.id.tv_name, R.id.tv_com1});
        listview.setAdapter(sdata);
    }

    public void btn_gc(View view) {
        View sq_a, sq_b;
        sq_a = findViewById(R.id.sq_a);
        sq_b = findViewById(R.id.sq_b);
        sq_a.setVisibility(View.VISIBLE);
        sq_b.setVisibility(View.GONE);
        Refresha();
        listview.setOnItemClickListener(this);
        listview.setOnItemLongClickListener(this);
    }

    public void btn_guanz(View view) {
        View sq_a, sq_b;
        sq_a = findViewById(R.id.sq_a);
        sq_b = findViewById(R.id.sq_b);
        sq_b.setVisibility(View.VISIBLE);
        sq_a.setVisibility(View.GONE);
        Refreshb();

        TextView man_name = (TextView) findViewById(R.id.textname);
        TextView man_id = (TextView) findViewById(R.id.textid);

        Bundle bundle = this.getIntent().getExtras();
        String manname = bundle.getString("manname");
        String manid = bundle.getString("manid");
        man_name.setText(manname);
        man_id.setText(manid);


        listview.setOnItemClickListener(this);
        listview.setOnItemLongClickListener(this);

    }

    public void btn_fabu(View view) {
        Intent intent = new Intent(Square.this, Square_add.class);    //收到消息后跳转
        Bundle bundle = this.getIntent().getExtras();
        String manname = bundle.getString("manname");
        String manid = bundle.getString("manid");
        Bundle bund = new Bundle();
        bund.putString("manname", manname);
        bund.putString("manid", manid);
        intent.putExtras(bund);
        startActivity(intent);
    }

    public void btn_den(View view) {
        Intent intent = new Intent(Square.this, Square_den.class);    //收到消息后跳转
        startActivity(intent);
    }


    // 点击某一个item
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        Intent intent = new Intent(Square.this, Square_xq.class);    //收到消息后跳转
        Map<String, String> itemMap = (Map<String, String>) listview.getItemAtPosition(arg2);
//        String i = itemMap.get("mid");
//        Toast.makeText(this, "id=1+" + arg2, Toast.LENGTH_SHORT).show();

        Bundle bundle = this.getIntent().getExtras();
        String manname = bundle.getString("manname");
        Bundle bund = new Bundle();
        bund.putString("manname", manname);

//        bund.putString("te1", Integer.toString(arg2 + 1));//a中可用

        String iid = itemMap.get("id");
        bund.putString("te1", iid);//a,b中可用

        intent.putExtras(bund);
        startActivity(intent);

    }


    // 长按item的点击事件
    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
                                   long arg3) {

        Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("删除该动态吗？");
        builder.setMessage("确认删除吗？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                Map<String, String> itemMap = (Map<String, String>) listview.getItemAtPosition(arg2);

                DBSquare Square = new DBSquare(Square.this);
                db = Square.getWritableDatabase();
                String iid = itemMap.get("id");
                db.delete("Square","id=?",new String[]{iid});


                Refreshb();
                Refresha();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.create();
        builder.show();
        return true;
    }


}